package com.example.dafafastlib.base;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.dafafastlib.R;
import com.example.dafafastlib.utils.StringUtils;
import com.zhy.autolayout.AutoLayoutActivity;
import com.zhy.autolayout.AutoRelativeLayout;

/**
 * Created by wang_dafa on 2018/1/30.
 */
public abstract class BaseFullScreenActivity extends AutoLayoutActivity {
    protected final String TAG = this.getClass().getSimpleName();
    private AutoRelativeLayout toolbar;
    private ImageView leftImg, rightImg;
    private TextView mTitle;
    private RelativeLayout rl_left, rl_right;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        if (android.os.Build.VERSION.SDK_INT > 18) {
            Window window = getWindow();
            //设置StatusBar为透明显示,需要在setContentView之前完成操作
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        initToolBar();
        initView();
        initListener();
        initData();
    }

    protected void initToolBar() {
        if (findViewById(R.id.toolbar) != null) {
            toolbar = (AutoRelativeLayout) findViewById(R.id.toolbar);
            leftImg = (ImageView) findViewById(R.id.toolbar_left_img);
            rightImg = (ImageView) findViewById(R.id.toolbar_right_img);
            mTitle = (TextView) findViewById(R.id.toolbar_title);
            rl_left = (RelativeLayout) findViewById(R.id.toolbar_rl_left);
            rl_right = (RelativeLayout) findViewById(R.id.toolbar_rl_right);
//            if (null != getSupportActionBar()) {
//                getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
//            }
        }
    }

    protected void setToolbarLeftIconClick(View.OnClickListener l) {
        if (null == rl_left) {
            return;
        }
        rl_left.setOnClickListener(l);
    }

    protected void setToolbarRightIconClick(View.OnClickListener l) {
        if (null == rl_right) {
            return;
        }
        rl_right.setOnClickListener(l);
    }

    protected void setMyTitle(String title) {
        if (null == mTitle) {
            return;
        }
        mTitle.setText(title);
    }

    protected void setMyTitle(int title) {
        if (null == mTitle) {
            return;
        }
        mTitle.setText(title);
    }

    protected void setTitleBgColor(int rid) {
        if (null == toolbar) {
            return;
        }
        toolbar.setBackgroundColor(rid);

    }

    protected void setToolbarLeftIconBg(int rid) {
        if (null == leftImg) {
            return;
        }
        leftImg.setBackgroundResource(rid);
    }

    protected void setToolbarRightIconBg(int rid) {
        if (null == rightImg) {
            return;
        }
        rightImg.setBackgroundResource(rid);
    }

    protected abstract void initData();

    protected abstract void initListener();

    protected abstract void initView();

    protected abstract int getLayoutId();

    protected ProgressDialog mProgressDialog;

    public ProgressDialog showProgress(String title, String message) {
        return showProgress(title, message, R.style.custom_dialog);
    }

    public ProgressDialog showProgress(String title, String message, int theme) {
        if (mProgressDialog == null) {
            if (theme > 0)
                mProgressDialog = new ProgressDialog(this, theme);
            else
                mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            mProgressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            mProgressDialog.setCanceledOnTouchOutside(false);// 不能取消
            mProgressDialog.setIndeterminate(true);// 设置进度条是否不明确
        }

        if (!StringUtils.isEmpty(title))
            mProgressDialog.setTitle(title);
        mProgressDialog.setMessage(message);
        mProgressDialog.show();
        return mProgressDialog;
    }

    public void hideProgress() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
    }
}
